###############################################################################
#								Global Makefile
###############################################################################

CC=g++
MODULES :=  src/tty \
			src/helper \
			src/interface \
			src/smart_home \
			src/expr_parser \
			src/udp_server \
			src/tcp_server \
			src/config_parser

# look for include files in each of the modules
CFLAGS += $(patsubst %,-I./%, $(MODULES)) -I./inc `pkg-config libxml++-2.6 \
		  --cflags --libs` -ggdb -lrt -lm -pthread -Wall

# extra libraries if required
LIBS :=

# each module will add to this
SRC :=

# include the description for each module
include $(patsubst %, %/module.mk,$(MODULES))

# determine the object files
OBJ := $(patsubst %.cpp,%.o, $(SRC))

# Build all
all: bin/smart_home bin/sh_client

bin/sh_client: src/sh_client/client.c
	$(CC) -o $@ $<

# link the program
bin/smart_home: $(OBJ) | bin
	$(CC) -o $@ $(OBJ) $(LIBS) $(CFLAGS)

# create bin directory
bin: 
	mkdir $@

# making object files from cpp
%.o: %.cpp
	$(CC) -c -o $@ $< $(CFLAGS) $(LIBS)

# include the CPP include dependencies
-include $(OBJ:.o=.d)

# calculate C include dependencies
%.d: %.cpp
	$(CC) -MM $(CFLAGS) $< | sed -e '1 s/[^:]*/$(subst /,\/,$(<:.cpp=.o))/' > $@ 

.PHONY: clean
clean:
	rm -rf bin/* `find -name *.o` `find -name *.d`
